package cn.ac.registAService.commons.util;

/** 
 * @author  yangzhong  E-mail:yangzhonglive@gmail.com
 * @version 1.0
 * @date    2012-12-3 下午4:07:54 
 * @fuction Just a container of 2 elements. Good for returning 2 values.
 */

public class Pair<L,R>
{
	/**
	 * A factory to create a new pair
	 * @param <L> The left component type
	 * @param <R> The right component type
	 * @param left The left component value
	 * @param right The right component value
	 * @return A new Pair object
	 */
	public static <L, R>  Pair<L, R> of(L left,R right)
	{
		return new Pair<L, R>(left, right);
	}
	
	/**
	 * A factory to create a new pair
	 * @param <L> The left component type
	 * @param <R> The right component type
	 * @param p A pair of objects
	 * @return A new Pair object
	 */
	public static <L, R> Pair<L, R> of(Pair<? extends L,? extends R> p)
	{
		return new Pair<L, R>(p);
	}
	
	
	
    private L left;
    private R right;
    
    /**
     * Constructor to create the Pair object
     * @param left The left component value
     * @param right The right component value
     */
    private Pair(L left,R right)
    {
    	this.left=left;
    	this.right=right;
    }
    
    /**
     * Constructor to create the Pair object
     * @param p A pair of objects
     */
    private Pair(Pair<? extends L,? extends R> p)
    {
    	left=p.getLeft();
    	right=p.getRight();
    }

	public L getLeft() {
		return left;
	}

	public R getRight() {
		return right;
	}

	//Just a container of 3 elements. Good for returning 3 values.
	public static class Three<L,R,C> extends Pair<L, R>
	{
		//A factory to create a new three
		public static <L,R,C> Three<L,R,C> of(L left,R right,C c)
		{
			return new Three<L,R,C>(left, right, c);
		}
		
		//constructor
		private Three(L left,R right,C c)
		{
			super(left, right);
			this.c=c;
		}
		
		private C c;
		
		public C getC(){
			return c;
		}
		
	}
	
	@Override
	public String toString() {
		return "Pair [left=" + left + ", right=" + right + "]";
	}
        
}
